package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.QiandaoRecord;
import com.xindaoapp.happypet.bean.QiandaoRecord.UserQD;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;



/**
 * 签到记录
 * 
 * @author
 * 
 */
public class QiandaoRecordActivity extends BaseActivity {

    private PullToRefreshListView vPullToRefreshListView;
    private TextView vContent;
    private ImageView isok;

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_qiandao;
    }



    @Override
    protected void initViews() {
        super.initViews();
        setTopBarTitle("签到记录");
        vContent = (TextView) findViewById(R.id.qd_content);
        vPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
        vPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        vPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });
        isok = (ImageView) findViewById(R.id.isok_img);
        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null && bundle.getBoolean("isOK")) {
            isok.setImageResource(R.drawable.qiandao_ok);
        }
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getQiandaoRecord(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<QiandaoRecord>() {

                    @Override
                    public void request(QiandaoRecord t) {

                        if (t != null) {
                            if (t.array.size() == 0) {
                                onDataArrivedEmpty();
                            } else {
                                onDataArrived(true);
                                vContent.setText(t.tip);
                                vPullToRefreshListView.getRefreshableView().setAdapter(
                                        new MyListAdapter(getApplicationContext(), t.array,
                                                Constants.PAGE_SIZE, R.layout.item_qiandao,
                                                R.layout.item_loading));
                            }

                        } else {
                            onDataArrived(false);
                        }
                        vPullToRefreshListView.onRefreshComplete();

                    }
                });
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    class MyListAdapter extends XinDaoBaseAdapter<UserQD> {

        public MyListAdapter(Context context, List<UserQD> iniData, int pageSize, int res,
                int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<UserQD> iLoadNextPageData) {
            // TODO Auto-generated method stub
            super.nextPage(start, size, iLoadNextPageData);
            HappyPetApplication.get().getMoccaApi()
                    .getQiandaoRecord(start, size, new IRequest<QiandaoRecord>() {

                        @Override
                        public void request(QiandaoRecord t) {
                            // TODO Auto-generated method stub
                            iLoadNextPageData.loadNextPageData(t == null ? null : t.array);
                        }
                    });

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent, UserQD value) {
            // TODO Auto-generated method stub
            ViewHolder holder = (ViewHolder) convertView.getTag();

            if (holder == null) {
                holder = new ViewHolder();
                holder.vTime = (TextView) convertView.findViewById(R.id.qd_time);
                holder.vCount = (TextView) convertView.findViewById(R.id.tv_weiba);
                convertView.setTag(holder);
            }


            holder.vTime.setText(CommonUtil.formatTimeForQiandao(value.addtime));
            holder.vCount.setText(value.reward);

            return convertView;
        }

    }

    class ViewHolder {
        TextView vTime;
        TextView vCount;
    }

}
